package com.cnunicom.modules.tra.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.cnunicom.common.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cnunicom.modules.tra.entity.ResDiscountEntity;
import com.cnunicom.modules.tra.service.ResDiscountService;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.R;


/**
 * 餐厅菜品表
 *
 * @author dzygod
 * @email dingziyuan0@gmail.com
 * @date 2020-03-04 09:39:59
 */
@RestController
@RequestMapping("tra/resdiscount")
public class ResDiscountController {
    @Autowired
    private ResDiscountService resDiscountService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("tra:resdiscount:list")
    public R list(@RequestParam Map<String, Object> params){
        List<ResDiscountEntity> list = resDiscountService.list();
        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("tra:resdiscount:info")
    public R info(@PathVariable("id") Integer id){
		ResDiscountEntity resDiscount = resDiscountService.getById(id);
        return R.ok().put("resDiscount", resDiscount);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("tra:resdiscount:save")
    public R save(@RequestBody ResDiscountEntity resDiscount){

        if (StringUtils.isNotBlank(resDiscount.getResName())) {
            resDiscount.setType(0);
            resDiscount.setParentId(0);
        } else {
            resDiscount.setType(1);
        }

        resDiscount.setCreateTime(DateUtils.getDateTime());
        resDiscount.setIsDel(0);
		resDiscountService.save(resDiscount);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("tra:resdiscount:update")
    public R update(@RequestBody ResDiscountEntity resDiscount){
		resDiscountService.updateById(resDiscount);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("tra:resdiscount:delete")
    public R delete(@RequestBody Integer[] ids){
		resDiscountService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
